package com.wenx.throwCamel;

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;
import net.sf.json.JSONArray;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.RouteDefinition;

import javax.jms.ConnectionFactory;

import static org.apache.camel.builder.Builder.constant;

/**
 * 通过虚拟主题持久化订阅
 */
public class httpThrow1 {
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        context.addRouteDefinition(initRoute());
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程`
        context.stop(); // 最后停止CamelContext
    }

    public static RouteDefinition initRoute(){
        RouteDefinition routeDefinition = new RouteDefinition();
        routeDefinition.from("activemq:queue:Consumer.A.VirtualTopic.test1")
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        String body = exchange.getIn().getBody().toString();
                        System.out.println("消费的body="+body);
                        ReadContext context = JsonPath.parse(body);
                        Object bodys = context.read("$.[?((@.id > 50 && @.name == '王五') || @.id == 123)]");
                        long totalCount = JSONArray.fromObject(bodys).size();
                        String objMap = "{\"totalCount\":" + totalCount + "," +
                                "\"data\":" +bodys.toString()+ "}";
                        exchange.getOut().setBody(objMap);
                    }
                })
                .setHeader(Exchange.HTTP_METHOD, constant("POST"))
                .setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
                .to("http://localhost:8080/demo/hello7");
        return routeDefinition;
    }
}
